The HTML tables allows to arrange data like text, images, links, other tables, etc. into rows and columns of cells.
1. Tables are defined with the <table> tag.
2. Tables are divided into table rows with the <tr> tag.
3. Table rows are divided into table data with the <td> tag.
4. A table row can also be divided into table headings with the <th>.
Example for HTML Tables :
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td { border: 1px solid black; border-collapse: collapse; }
</style>
</head>
<body>
<table style="width:50%">
<tr>
<th>Country</th>
<th>Capital</th>
</tr>
<tr>
<td>Nigeria</td>
<td> Abuja</td>
</tr>
<tr>
<td>Saudi Arabia</td>
<td>Makka</td>
</tr>
<tr>
<td>Niger </td>
<td>Miami</td>
</tr>
</table>
</body>
</html>
Table with Cell padding :
Cell padding specifies the space between the cell content and its borders, If not specified a padding, the table cells will be displayed without padding.
Example for Cell Padding :
<!DOCTYPE html>
<html>
<head
> <style>
table, th, td { border: 1px solid black; border-collapse: collapse; }
th, td { padding: 15px; }
</style>
</head>
<body>
<table style="width:50%">
<tr>
<th>ITEMS</th>
<th>PRICES</th>
</tr>
<tr>
<td>ORANGE</td>
<td>$0.4</td>
</tr>
<tr>
<td>APPLE</td>
<td>$0.6</td>
</tr>
<tr>
<td>MANGO</td>
<td>$0.3</td>
</tr>
</table>
<p>The cell padding is set to 15px.</p> </body>
</html>
TABLE WITH BORDER SPACING :
Border spacing specifies the space between the cells.
Example for Border Spacing :
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td { border: 1px solid black; padding: 5px; }
table { border-spacing: 15px; }
</style>
</head>
<body>
<table style="width:50%">
<tr>
<th>Country</th>
<th>capital</th>
</tr>
<tr>
<td>India</td>
<td>New delhi</td>
</tr>
<tr>
<td>Germany</td>
<td>Berlin</td>
</tr>
<tr>
<td>New zealand</td> <td>Wellington</td>
</tr>
</table>
<p> border-spacing is set to 15px.</p> </body>
</html>
CELL THAT SPAN MANY COLUMNS :
To make a cell span more than one column, colspan attribute should be used .
Example for Cell that span many columns :
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td { border: 1px solid black; border-collapse: collapse; }
th, td { padding: 5px; text-align: left; }
</style>
</head>
<body>
<h2>Cell that spans two columns:</h2> <table style="width:100%"> <tr> <th>Name</th>
<th colspan="2">Telephone</th>
</tr>
<tr>
<td>xyz</td>
<td>123455...</td>
<td>abcdef...</td>
</tr>
</table>
</body>
</html>
CELL THAT SPAN MANY ROWS:
To make a cell span more than one row, use the rowspan attribute.
Example for cell that span many rows :
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td { border: 1px solid black; border-collapse: collapse; }
th, td { padding: 5px; text-align: left; }
</style>
</head>
<body>
<h2>Cell that spans two rows:</h2> <table style="width:100%">
<tr>
<th>Name:</th>
<td>xyz</td>
</tr>
<tr>
<th rowspan="2">Telephone:</th> <td>123456...</td>
</tr>
<tr>
<td>abcdef...</td>
</tr>
</table>
</body>
</html>
TABLE WITH CAPTION :
To add a caption to a table, use the <caption> tag.
Example for Table with caption :
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td { border: 1px solid black; border-collapse: collapse; }
th, td { padding: 5px; text-align: left; }
</style>
</head>
<body>
<table style="width:50%">
<caption> CAPITALS </caption>
<tr>
<th>Country</th>
<th>capital</th>
</tr>
<tr>
<td>India</td>
<td>New delhi</td>
</tr>
<tr>
<td>Germany</td>
<td>Berlin</td>
</tr>
<tr>
<td>New zealand</td>
<td>Wellington</td>
</tr>
</table>
</body>
</html>
DIFFERENT STYLES FOR DIFFERENT TABLES :
To define a different style for a different table, add an id attribute to the table .
Example for Different Styles :
<!DOCTYPE html>
<html>
<head>
<style>
table { width:50%; }
table, th, td { border: 1px solid black; border-collapse: collapse; }
th, td { padding: 5px; text-align: left; } table#t01 tr:nth-child(even) { background-color: #6B9BC3; } table#t01 tr:nth-child(odd) { background-color:#6DDAD6 ; } table#t01 th { background-color: #6DDAD6 ; color: black; }
</style>
</head>
<body>
<table id="t01">
<tr>
<th>Nick Name</th>
<th>First Name</th>
</tr>
<tr>
<td>zunnurrain </td>
<td>usman</td>
</tr>
<tr>
<td>Amirul muminiin</td>
<td>Umar</td>
</tr>
<tr>
<td>assidiq</td>
<td>Abubakar</td>
</tr>
</table>
</body>
</html>
TABLE COLOR
To define color for the borders, and the text and background color of elements
Example for Table color :
<!DOCTYPE html>
<html>
<head>
<style>
table, td, th { border: 1px solid steelblue; }
th { background-color: #6DDAD6; color: black; }
</style>
</head>
<body>
<table>
<tr>
<th>Nick Name</th>
<th>First Name</th>
</tr>
<tr>
<td>zunnurrain </td>
<td>usman</td>
</tr>
<tr>
<td>Amirul muminiin</td>
<td>Umar</td>
</tr>
<tr>
<td>assidiq</td>
<td>Abubakar</td>
</tr>
</table>
</body>
</html>
To be continue NEXT POST...
Thanks for reading.
Comments